//
//  KVNHomeNewsItem.m
//  Test1
//
//  Created by KelvinYeung on 15/7/18.
//  Copyright (c) 2015年 KelvinYeung. All rights reserved.
//

#import "KVNHomeNewsItem.h"


@interface KVNHomeNewsItem() <UIGestureRecognizerDelegate>

@property (nonatomic,copy) void (^clickAction)(NSObject *obj);


@end


@implementation KVNHomeNewsItem

@synthesize titleLabel;
@synthesize contentLabel;
@synthesize imageView;
@synthesize data;
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (instancetype) init
{
    self = [super init];
    [self initSubViews];
    
    return self;
}

- (instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    //[self initSubViews];
    return self;
    
}

- (void) initSubViews
{
    titleLabel = [[UILabel alloc] init];
    contentLabel = [[UILabel alloc] init];
    imageView = [[UIImageView alloc] init];
    
    
    contentLabel.textColor = [UIColor lightGrayColor];
    
    
    [self addSubview:titleLabel];
    [self addSubview:contentLabel];
    [self addSubview:imageView];
    
    //边框
    self.layer.borderWidth = 0.5;
    self.layer.borderColor = [[UIColor colorWithWhite:0.8 alpha:1] CGColor];
    
    //点击
    UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleFingerEvent:)];
    singleFingerOne.numberOfTouchesRequired = 1; //手指数
    singleFingerOne.numberOfTapsRequired = 1; //tap次数
    singleFingerOne.delegate = self;
    [self addGestureRecognizer:singleFingerOne];
    
    //
    
}


- (void) drawRect:(CGRect)rect
{
    //NSLog(@"drawRect::::::::::");
    //titleLabel.text = _title;
    //contentLabel.text=_content;
    //imageView.image = _image;
    
    //titleLabel.backgroundColor = [UIColor redColor];
    //contentLabel.backgroundColor = [UIColor whiteColor];
    //imageView.backgroundColor = [UIColor lightGrayColor];
    
    float imageDiameter = rect.size.height * 0.70;
    
    imageView.frame = CGRectMake(rect.size.width - imageDiameter-5, (rect.size.height-imageDiameter)*0.5, imageDiameter, imageDiameter);
    //imageView.layer.masksToBounds = YES;
    //imageView.layer.cornerRadius = imageDiameter * 0.5;
    
    titleLabel.frame = CGRectMake(10, rect.size.height*0.15f, rect.size.width - imageDiameter - 10 , 30);
    
    contentLabel.frame = CGRectMake(10,rect.size.height*0.45f, rect.size.width - imageDiameter - 10 , 30);
    [contentLabel setFont:[UIFont systemFontOfSize:12.0]];
    
    [super drawRect:rect];
    //[imageView drawRect:imageView.frame];
}



#pragma mark - UIView点击事件
- (void) handleSingleFingerEvent:(UITapGestureRecognizer *) sender{
    

    
}

- (void) setData:(NSDictionary *)aData{
    titleLabel.text = [aData objectForKey:@"title"];
    contentLabel.text = [aData objectForKey:@"content"];
    [imageView setImageFromURL:[NSURL URLWithString:[aData objectForKey:@"icon"]] placeHolderImage:[UIImage imageNamed:@"home_logo"] animation:YES];

}


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    self.backgroundColor=[UIColor lightTextColor];
    
}

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
    self.backgroundColor=[UIColor whiteColor];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    self.backgroundColor=[UIColor whiteColor];
}

@end
